package com.feicheng.service.a;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.task.Cache;
import com.feicheng.common.util.StringGenerator;
import com.feicheng.persistence.entity.*;
import com.feicheng.persistence.mapper.*;
import com.feicheng.persistence.model.SkjlModel;
import com.feicheng.persistence.service.*;
import com.feicheng.wxapi.pay.WxAppPayBean;
import com.feicheng.wxapi.pay.transfer.model.TransferBillsResponse;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class FDzService {

    @Autowired
    Cache cache;
    private String appid;

    private Integer yj;

    @Autowired
    IAdzService adzService;

    @Autowired
    IAmdjlService amdjlService;

    @Autowired
    IAmenjinService menjinService;

    @Autowired
    IAmountService amountService;

    @Autowired
    IAkechengitemService kcitemService;

    @Autowired
    WxAppPayBean wxAppPayBean;

    @Autowired
    IAroomyyjlService yyjlService;

    @Autowired
    IAkechengService kechengService;

    @Autowired
    IAstoreService storeService;

    @Autowired
    IAroomService roomService;

    @PostConstruct
    private void init() {
        Asetting asetting = cache.getObject("Asetting", Asetting.class);
        appid = asetting.getDappid();
        yj = asetting.getDzyj();
    }

    public JSONObject menjinHeartBeat(String mjid, Boolean statu) {
        JSONObject res = new JSONObject();
        Amenjin amenjin = new Amenjin();
        amenjin.setMjid(mjid);
        amenjin.setStatu(statu);
        menjinService.updateById(amenjin);
        return res;
    }

    /**
     * 店长扫脸接口
     * @param param
     * @return
     */
    public JSONObject sl(JSONObject param) {
        String dzid = param.getString("dzid");
        String mjid = param.getString("mjid");

        if (!StringUtils.hasLength(dzid)) {
            throw new SystemException(dzid + "：店长没有被找到");
        }

        if (!StringUtils.hasLength(mjid)) {
            throw new SystemException(mjid + "：门禁没有被找到");
        }
        Amenjin menjin = menjinService.getById(mjid);
        Astore astore = storeService.getById(menjin.getStoreid());
        if (!dzid.equals(astore.getDzid())) {
            throw new SystemException(dzid+"店长和门店"+menjin.getStoreid()+"权限不匹配");
        }

        return param;
    }

    public JSONObject mdjlsl(JSONObject param) {
        String mdjlid = param.getString("mdjlid");
        String mjid = param.getString("mjid");

        if (!StringUtils.hasLength(mdjlid)) {
            throw new SystemException(mdjlid + "：教练没有被找到");
        }

        if (!StringUtils.hasLength(mjid)) {
            throw new SystemException(mjid + "：门禁没有被找到");
        }
        Amenjin menjin = menjinService.getById(mjid);
        Amdjl amdjl = amdjlService.getById(mdjlid);

        if (!menjin.getStoreid().equals(amdjl.getStoreid())) {
            throw new SystemException(mdjlid+"门店教练和门店"+menjin.getStoreid()+"权限不匹配");
        }

        return param;
    }

    /**
     * 店长绑定门店
     * @param param
     * @return
     */
    public JSONObject bind(JSONObject param) {

        String akey = param.getString("akey");
        String dzid = param.getString("dzid");

        if(!StringUtils.hasLength(akey)) {
            throw new SystemException("密钥必须");
        }

        QueryWrapper<Astore> qws = new QueryWrapper<>();
        qws.eq("akey", akey);

        Astore astore = storeService.getOne(qws, false);

        if (null == astore) {
            throw new SystemException("密钥填写错误");
        }


        astore.setDzid(dzid);
        storeService.updateById(astore);
        param.put("astore", astore);
        return param;
    }

    /**
     * 店长查询接口  骏哥
     * @param param
     * @return
     */
    public JSONObject getDz(JSONObject param) {

        String dzid = param.getString("dzid");
        Adz dz = adzService.getById(dzid);
        param.put("adz", dz);

        Amount amount = amountService.getById(dz.getOpenid());
        param.put("amount", amount);

        QueryWrapper<Astore> dzqw = new QueryWrapper<>();
        dzqw.eq("dzid", dzid);
        List<Astore> stores = storeService.list(dzqw);
        param.put("stores", stores);

        Set<String> mdids = new HashSet<>();
        stores.forEach(md -> {
            mdids.add(md.getStoreid());
        });

        Long fjnumber = 0L;
        Long yynumber = 0L;
        if (mdids.size() > 0) {
            QueryWrapper<Aroom> rmqw = new QueryWrapper<>();
            rmqw.in("storeid", mdids);
            fjnumber = roomService.count(rmqw);

            QueryWrapper<Aroomyyjl> yyqw = new QueryWrapper<>();
            yyqw.in("storeid", mdids);
            yynumber = yyjlService.count(yyqw);
            param.put("mdids", mdids);
        }
        param.put("fjnumber", fjnumber);
        param.put("yynumber", yynumber);

        return param;
    }

    /**
     * 店长分页查询接口  颖姐
     * @param param
     * @return
     */
    public JSONObject getDzsByPage(JSONObject param) {

        Integer current = param.getInteger("current");
        Integer size = param.getInteger("size");

        if(current == null || current < 0){
            current = 1;
        }

        if(size == null || size < 0){
            size = 16;
        }

        //构建分页
        IPage<JSONObject> page = new Page<JSONObject>(current, size);

        //构建条件
        QueryWrapper<JSONObject> qwdzpage = new QueryWrapper<>();

        String dname = param.getString("dname");
        if(StringUtils.hasLength(dname)){
            qwdzpage.like("adz.dname", dname);
        }

        String tel = param.getString("tel");
        if(StringUtils.hasLength(tel)){
            qwdzpage.like("adz.tel", tel);
        }

        String sname = param.getString("sname");
        if(StringUtils.hasLength(sname)){
            qwdzpage.like("adz.tel", sname);
        }

        AdzMapper adzMapper = (AdzMapper) adzService.getBaseMapper();
        page = adzMapper.getDzsByPage(page, qwdzpage);
        param.put("dzpage", page);

        return param;
    }

    /**
     * 店长详细查询 颖姐
     * @param param
     * @return
     */
    public JSONObject getDzDetail(JSONObject param) {

        String dzid = param.getString("dzid");
        if (!StringUtils.hasLength(dzid)) {
            throw new SystemException("店长id必须指定");
        }
        //店长信息
        //店长表
        Adz adz = adzService.getById(dzid);
        param.put("adz", adz);

        return param;
    }


    public JSONObject updateDz(JSONObject param) {
        String dzid = param.getString("dzid");

        Integer yj = param.getInteger("yj");

        if (!StringUtils.hasLength(dzid)) {
            throw new SystemException("店长id必须指定");
        }
        Adz adz = adzService.getById(dzid);
        adz.setYj(yj);
        adz.setDzid(dzid);
        adzService.updateById(adz);
        param.put("adz", adz);
        return param;
    }
}
